/**
 * CRM模块API接口
 */

import request from '@/utils/request'
import type {
  Customer,
  Contact,
  Followup,
  Order,
  Payment,
  Review,
  PaginationParams,
  QueryParams,
  ListResponse,
  ApiResponse,
} from '@/types/crm'

// 客户管理API
export const customerApi = {
  // 获取客户列表
  list: (params: PaginationParams & QueryParams): Promise<ApiResponse<ListResponse<Customer>>> => {
    return request({
      url: '/api/crm/customer/list',
      method: 'get',
      params,
    })
  },

  // 获取客户详情
  detail: (id: number): Promise<ApiResponse<Customer>> => {
    return request({
      url: `/api/crm/customer/detail/${id}`,
      method: 'get',
    })
  },

  // 新增客户（需要审核）
  create: (data: Customer): Promise<ApiResponse> => {
    return request({
      url: '/api/crm/customer/add',
      method: 'post',
      data,
    })
  },

  // 新增客户（别名方法）
  add: (data: Customer): Promise<ApiResponse> => {
    return request({
      url: '/api/crm/customer/add',
      method: 'post',
      data,
    })
  },

  // 编辑客户（需要审核）
  edit: (data: Customer): Promise<ApiResponse> => {
    return request({
      url: '/api/crm/customer/edit',
      method: 'put',
      data,
    })
  },

  // 更新客户（别名方法）
  update: (data: Customer): Promise<ApiResponse> => {
    return request({
      url: '/api/crm/customer/edit',
      method: 'put',
      data,
    })
  },

  // 删除客户
  delete: (id: number): Promise<ApiResponse> => {
    return request({
      url: `/api/crm/customer/delete/${id}`,
      method: 'delete',
    })
  },

  // 转移客户到公海
  toPublic: (id: number): Promise<ApiResponse> => {
    return request({
      url: `/api/crm/customer/toPublic/${id}`,
      method: 'put',
    })
  },

  // 标记为已合作
  markCooperation: (id: number): Promise<ApiResponse> => {
    return request({
      url: `/api/crm/customer/markCooperation/${id}`,
      method: 'put',
    })
  },

  // 获取客户选项
  getCustomerOptions: () => {
    return request.get<Array<{ value: number; label: string }>>('/api/crm/customer/options')
  },

  // 获取客户审核列表
  getReviewList: (
    params: PaginationParams & QueryParams
  ): Promise<ApiResponse<ListResponse<Review>>> => {
    return request({
      url: '/api/crm/customer/reviewList',
      method: 'get',
      params,
    })
  },

  // 审核客户
  approveReview: (data: { id: number; status: number; remark?: string }): Promise<ApiResponse> => {
    return request({
      url: '/api/crm/customer/reviewApprove',
      method: 'post',
      data,
    })
  },

  // 获取当前用户的审核记录
  myReviews: (
    params: PaginationParams & QueryParams
  ): Promise<ApiResponse<ListResponse<Review>>> => {
    return request({
      url: '/api/crm/customer/myReviews',
      method: 'get',
      params,
    })
  },

  // 撤销审核申请
  cancelReview: (reviewId: number, category: string = 'customer'): Promise<ApiResponse> => {
    return request({
      url: '/api/crm/customer/cancelReview',
      method: 'post',
      data: { review_id: reviewId, category },
    })
  },

  // 获取行业选项
  industryOptions: (): Promise<ApiResponse<Array<{ value: number; label: string }>>> => {
    return request({
      url: '/api/crm/customer/industryOptions',
      method: 'get',
    })
  },

  // 获取企业规模选项
  scaleOptions: (): Promise<ApiResponse<Array<{ value: number; label: string }>>> => {
    return request({
      url: '/api/crm/customer/scaleOptions',
      method: 'get',
    })
  },

  // 获取客户来源选项
  sourceOptions: (): Promise<ApiResponse<Array<{ value: number; label: string }>>> => {
    return request({
      url: '/api/crm/customer/sourceOptions',
      method: 'get',
    })
  },

  // 获取负责人选项
  userOptions: (): Promise<
    ApiResponse<Array<{ value: number; label: string; department_id?: number }>>
  > => {
    return request({
      url: '/api/crm/customer/userOptions',
      method: 'get',
    })
  },

  // 获取部门选项
  departmentOptions: (): Promise<ApiResponse<Array<{ value: number; label: string }>>> => {
    return request({
      url: '/api/crm/customer/departmentOptions',
      method: 'get',
    })
  },
}

// 联系人管理API
export const contactApi = {
  // 获取联系人列表
  list: (customerId: number): Promise<ApiResponse<Contact[]>> => {
    return request({
      url: `/api/crm/contact/list/${customerId}`,
      method: 'get',
    })
  },

  // 新增联系人
  create: (data: Contact): Promise<ApiResponse> => {
    return request({
      url: '/api/crm/contact/create',
      method: 'post',
      data,
    })
  },

  // 更新联系人（需要审核）
  update: (data: Contact): Promise<ApiResponse> => {
    return request({
      url: '/api/crm/contact/update',
      method: 'put',
      data,
    })
  },

  // 删除联系人（需要审核）
  delete: (id: number): Promise<ApiResponse> => {
    return request({
      url: `/api/crm/contact/delete/${id}`,
      method: 'delete',
    })
  },

  // 获取联系人审核列表
  getReviewList: (
    params: PaginationParams & QueryParams
  ): Promise<ApiResponse<ListResponse<Review>>> => {
    return request({
      url: '/api/crm/contact/reviewList',
      method: 'get',
      params,
    })
  },

  // 审核联系人
  approveReview: (data: { id: number; status: number; remark?: string }): Promise<ApiResponse> => {
    return request({
      url: '/api/crm/contact/reviewApprove',
      method: 'post',
      data,
    })
  },
}

// 跟进记录管理API
export const followupApi = {
  // 获取跟进记录列表
  list: (params: PaginationParams & QueryParams): Promise<ApiResponse<ListResponse<Followup>>> => {
    return request({
      url: '/api/crm/followup/list',
      method: 'get',
      params,
    })
  },

  // 新增跟进记录
  create: (data: Followup): Promise<ApiResponse> => {
    return request({
      url: '/api/crm/followup/create',
      method: 'post',
      data,
    })
  },

  // 更新跟进记录
  update: (data: Followup): Promise<ApiResponse> => {
    return request({
      url: '/api/crm/followup/update',
      method: 'put',
      data,
    })
  },

  // 删除跟进记录
  delete: (id: number): Promise<ApiResponse> => {
    return request({
      url: `/api/crm/followup/delete/${id}`,
      method: 'delete',
    })
  },

  // 获取跟进记录详情
  detail: (id: number, config?: any) => {
    return request.get<Followup>(`/api/crm/followup/detail/${id}`, config)
  },

  // 获取跟进选项
  getFollowupOptions: () => {
    return request.get<Array<{ value: number; label: string }>>('/api/crm/followup/options')
  },
}

// 业务订单管理API
export const orderApi = {
  // 获取订单列表
  list: (params: PaginationParams & QueryParams): Promise<ApiResponse<ListResponse<Order>>> => {
    return request({
      url: '/api/crm/order/list',
      method: 'get',
      params,
    })
  },

  // 获取订单详情
  detail: (id: number): Promise<ApiResponse<Order>> => {
    return request({
      url: `/api/crm/order/detail/${id}`,
      method: 'get',
    })
  },

  // 新增订单
  create: (data: Order): Promise<ApiResponse> => {
    return request({
      url: '/api/crm/order/add',
      method: 'post',
      data,
    })
  },

  // 更新订单
  update: (data: Order): Promise<ApiResponse> => {
    return request({
      url: '/api/crm/order/edit',
      method: 'put',
      data,
    })
  },

  // 删除订单
  delete: (id: number): Promise<ApiResponse> => {
    return request({
      url: `/api/crm/order/delete/${id}`,
      method: 'delete',
    })
  },

  // 获取订单选项（所有）
  getOrderOptions: () => {
    return request.get<Array<{ value: string; label: string }>>('/api/crm/order/options')
  },

  // 获取订单类型选项
  typeOptions: () => {
    return request.get<Array<{ value: number; label: string }>>('/api/crm/order/typeOptions')
  },

  // 获取订单状态选项
  statusOptions: () => {
    return request.get<Array<{ value: number; label: string }>>('/api/crm/order/statusOptions')
  },

  // 获取订单状态选项（兼容旧方法）
  getStatusOptions: () => {
    return request.get<Array<{ value: number; label: string }>>('/api/crm/order/statusOptions')
  },

  // 获取付款方式选项
  getPaymentMethodOptions: () => {
    return request.get<Array<{ value: string; label: string }>>(
      '/api/crm/order/paymentMethodOptions'
    )
  },

  // 更新订单状态
  updateStatus: (id: number, status: number) => {
    return request.put(`/api/crm/order/updateStatus/${id}`, { status })
  },

  // 获取订单付款记录
  getPayments: (id: number) => {
    return request.get<Payment[]>(`/api/crm/order/payments/${id}`)
  },

  // 添加付款记录
  addPayment: (data: Omit<Payment, 'id'>) => {
    return request.post<Payment>('/api/crm/payment/create', data)
  },

  // 获取订单审核记录
  getReviews: (id: number) => {
    return request.get<Review[]>(`/api/crm/order/reviews/${id}`)
  },

  // 添加审核记录
  addReview: (data: Omit<Review, 'id'>) => {
    return request.post<Review>('/api/crm/review/create', data)
  },
}

/**
 * 付款记录API
 */
export const paymentApi = {
  // 获取付款记录列表
  list: (params: QueryParams & PaginationParams) => {
    return request.get<ListResponse<Payment>>('/api/crm/payment/list', { params })
  },

  // 获取付款记录详情
  detail: (id: number) => {
    return request.get<Payment>(`/api/crm/payment/detail/${id}`)
  },

  // 创建付款记录
  create: (data: Omit<Payment, 'id'>) => {
    return request.post<Payment>('/api/crm/payment/create', data)
  },

  // 更新付款记录
  update: (id: number, data: Partial<Payment>) => {
    return request.put<Payment>(`/api/crm/payment/update/${id}`, data)
  },

  // 删除付款记录
  delete: (id: number) => {
    return request.delete(`/api/crm/payment/delete/${id}`)
  },

  // 获取付款选项
  getPaymentOptions: () => {
    return request.get<Array<{ value: string; label: string }>>('/api/crm/payment/options')
  },
}

/**
 * 审核记录API
 */
export const reviewApi = {
  // 获取审核记录列表
  list: (params: QueryParams & PaginationParams) => {
    return request.get<ListResponse<Review>>('/api/crm/review/list', { params })
  },

  // 获取审核记录详情
  detail: (id: number) => {
    return request.get<Review>(`/api/crm/review/detail/${id}`)
  },

  // 创建审核记录
  create: (data: Omit<Review, 'id'>) => {
    return request.post<Review>('/api/crm/review/create', data)
  },

  // 更新审核记录
  update: (id: number, data: Partial<Review>) => {
    return request.put<Review>(`/api/crm/review/update/${id}`, data)
  },

  // 删除审核记录
  delete: (id: number) => {
    return request.delete(`/api/crm/review/delete/${id}`)
  },

  // 获取审核选项
  getReviewOptions: () => {
    return request.get<Array<{ value: number; label: string }>>('/api/crm/review/options')
  },

  // 获取审核类型选项
  getReviewTypeOptions: () => {
    return request.get<Array<{ value: number; label: string }>>('/api/crm/review/typeOptions')
  },

  // 获取审核状态选项
  getReviewStatusOptions: () => {
    return request.get<Array<{ value: number; label: string }>>('/api/crm/review/statusOptions')
  },

  // 获取联系人审核列表
  contactList: (params: QueryParams & PaginationParams) => {
    return request.get<ListResponse<Review>>('/api/crm/contact/reviewList', { params })
  },
}

/**
 * 客户审核API
 */
export const customerReviewApi = {
  // 获取审核列表
  list: (params: QueryParams & PaginationParams) => {
    return request({
      url: '/api/crm/customerReview/list',
      method: 'get',
      params,
    })
  },

  // 获取审核详情
  detail: (id: number, category: string) => {
    return request({
      url: '/api/crm/customerReview/detail',
      method: 'get',
      params: { id, category },
    })
  },

  // 审核处理
  approve: (data: { id: number; category: string; status: number; remark?: string }) => {
    return request({
      url: '/api/crm/customerReview/approve',
      method: 'post',
      data,
    })
  },

  // 获取统计信息
  statistics: () => {
    return request({
      url: '/api/crm/customerReview/statistics',
      method: 'get',
    })
  },
}

/**
 * 订单审核API
 */
export const orderReviewApi = {
  // 获取审核列表
  list: (params: QueryParams & PaginationParams) => {
    return request.get<ListResponse<Review>>('/api/crm/orderReview/list', params)
  },

  // 获取审核详情
  detail: (id: number) => {
    return request.get<Review>(`/api/crm/orderReview/detail`, { id })
  },

  // 提交审核申请
  submit: (data: any) => {
    return request.post('/api/crm/orderReview/submit', data)
  },

  // 审核处理
  approve: (data: { id: number; status: number; remark?: string }) => {
    return request.post('/api/crm/orderReview/approve', data)
  },

  // 获取统计信息
  statistics: () => {
    return request.get('/api/crm/orderReview/statistics')
  },

  // 获取业务类型选项
  businessTypeOptions: () => {
    return request.get<Array<{ value: number; label: string }>>(
      '/api/crm/orderReview/businessTypeOptions'
    )
  },

  // 获取审核类型选项
  reviewTypeOptions: () => {
    return request.get<Array<{ value: number; label: string }>>(
      '/api/crm/orderReview/reviewTypeOptions'
    )
  },
}

// 统计API
export const statsApi = {
  // 获取CRM统计信息
  dashboard: (): Promise<ApiResponse> => {
    return request({
      url: '/api/crm/statistics/overview',
      method: 'get',
    })
  },

  // 获取客户统计
  customerStats: (): Promise<ApiResponse> => {
    return request({
      url: '/api/crm/statistics/followupTrend',
      method: 'get',
    })
  },

  // 获取订单统计
  orderStats: (): Promise<ApiResponse> => {
    return request({
      url: '/api/crm/statistics/orderAnalysis',
      method: 'get',
    })
  },

  // 获取待办事项
  getTodos: (): Promise<ApiResponse> => {
    return request({
      url: '/api/crm/statistics/todos',
      method: 'get',
    })
  },

  // 获取最近跟进记录
  getRecentFollowups: (): Promise<ApiResponse> => {
    return request({
      url: '/api/crm/statistics/recentFollowups',
      method: 'get',
    })
  },
}

// 导入并导出诉讼法务订单API
import { lawsuitLegalOrderApi } from './crm/lawsuitLegalOrderApi'

// 导出诉讼法务订单API
export { lawsuitLegalOrderApi }
